﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
// using Coravel.Mailer.Mail;
using Microsoft.EntityFrameworkCore;

namespace CRM.Models.Email
{
    [Index(nameof(EmailAddress))]
    [Index(nameof(CreatedAt))]
    public class EmailConfig : BaseModel
    {
        private EmailTypeEnum _emailType;
        
        [Required]
        [Comment("授权类型")]
        public EmailTypeEnum EmailType
        {
            get => _emailType;
            set
            {
                _emailType = value;
                // InitConfig(this, value);
            }
        }

        public static void InitConfig(EmailConfig emailConfig, EmailTypeEnum emailTypeEnum)
        {
            switch (emailTypeEnum)
            {
                case EmailTypeEnum.QQ:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.qq.com";
                    emailConfig.Port = 25;
                    break;
                case EmailTypeEnum.Netease126:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.126.com";
                    emailConfig.Port = 25;
                    break;
                case EmailTypeEnum.Netease163:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.163.com";
                    emailConfig.Port = 25;
                    break;
                 case EmailTypeEnum.Sina:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.sina.com";
                    emailConfig.Port = 25;
                    break;
                case EmailTypeEnum.Sohu:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.sohu.com";
                    emailConfig.Port = 25;
                    break; 
                case EmailTypeEnum.Gmail:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.gmail.com";
                    emailConfig.Port = 465;
                    break;
                case EmailTypeEnum.Hotmail:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.live.com";
                    emailConfig.Port = 25;
                    break;

                // case EmailTypeEnum.Other:
                default:
                    // InitSqlObjects.Add(New MailType("21cn.com", "smtp.21cn.com", False, 25))
                    break;
            }
        }
        
        [Comment("id")]
        public int Id { get; set; }
        
        [Comment("UserId")]
        public int UserId { get; set; }
        
        [Comment("GroupId")]
        public int GroupId { get; set; }
        
        [Comment("CompanyId")]
        public int CompanyId { get; set; }
        
        [Comment("OemId")]
        public int OemId { get; set; }

        [Column("EmailGroupId")]
        public int? EmailConfigGroupId { get; set; }
        public virtual EmailConfigGroup EmailConfigGroup { get; set; }
        
        [Comment("协议")]
        public string Driver { get; set; }
        
        [Comment("域名")]
        public string Host { get; set; }
        
        [Comment("端口")]
        public int Port { get; set; }
        
        [Required, EmailAddress]
        [Comment("邮箱地址")]
        public string EmailAddress { get; set; }
        
        /// <summary>
        /// 名称
        /// </summary>
        [Comment("名称"), MaxLength(50)]
        public string Name { get; set; }

        /// <summary>
        /// 邮箱授权验证状态
        /// </summary>
        [Comment("邮箱授权验证状态")]
        public EmailVerification State { get; set; } = EmailVerification.Unverified;
        
        [Required]
        [Comment("密钥")]
        public string Secret { get; set; }

        [Comment("开启ssl")]
        public bool Ssl { get; set; }

        // [NotMapped] public MailRecipient EmailAddressRecipient => new MailRecipient(EmailAddress);

        public enum EmailTypeEnum
        {
            Other,
            QQ,
            Netease126,
            Netease163,
            Sina,
            Sohu,
            Gmail,
            Hotmail
        }
        
        /// <summary>
        /// 最后一次发送时间
        /// </summary>
        [Comment("最后一次发送时间")]
        public DateTime? LatestSendTime { get; set; }

        /// <summary>
        /// 24小时内发件数量
        /// </summary>
        [Comment("24小时内发件数量")]
        public int LastDaySendNum { get; set; }

        /// <summary>
        /// 24小时内最大发件数量
        /// </summary>
        [Comment("24小时内最大发件数量")]
        public int MaxLastDaySendNum { get; set; }
    }

    /// <summary>
    /// smtp邮箱授权验证状态
    /// </summary>
    public enum EmailVerification
    {
        /// <summary>
        /// 未验证
        /// </summary>
        Unverified,
        Success,
        Failed,
    }
}